package com.ruoyi.delivery.controller;

import com.ruoyi.delivery.domain.DelOutBoundDetail;
import com.ruoyi.delivery.domain.DelOutBoundOrder;
import com.ruoyi.delivery.service.IDelOutBoundOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 出库订单 控制层
 *
 */
@RestController
@RequestMapping("/delivery/outBoundOrder")
public class DelOutBoundOrderController {

    @Autowired
    private IDelOutBoundOrderService outBoundOrderService;

    /**
     * 获取出库订单列表，支持分页和筛选
     *
     * @param outId 出库单号（模糊搜索）
     * @param warehouseId 仓库号（模糊搜索）
     * @param destination 目的地
     * @param outcomeDateStart 出库日期范围的起始日期
     * @param outcomeDateEnd 出库日期范围的结束日期
     * @param expectedDeliveryTimeStart 预计到达日期范围的起始日期
     * @param expectedDeliveryTimeEnd 预计到达日期范围的结束日期
     * @param state 出库单状态
     * @param evidenceRecordId 订单号（模糊搜索）
     * @param page 页码
     * @param pageSize 每页记录数
     * @return 分页出库订单列表
     */
    @GetMapping("/list")
    public Map<String, Object> getOutBoundOrderList(
            @RequestParam(required = false) String outId,
            @RequestParam(required = false) String warehouseId,
            @RequestParam(required = false) String destination,
            @RequestParam(required = false) String outcomeDateStart,
            @RequestParam(required = false) String outcomeDateEnd,
            @RequestParam(required = false) String expectedDeliveryTimeStart,
            @RequestParam(required = false) String expectedDeliveryTimeEnd,
            @RequestParam(required = false) String state,
            @RequestParam(required = false) String evidenceRecordId,
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int pageSize
    ) {
        List<DelOutBoundOrder> orders = outBoundOrderService.filterOutBoundOrders(
                outId, warehouseId, destination, outcomeDateStart, outcomeDateEnd, expectedDeliveryTimeStart, expectedDeliveryTimeEnd, state, evidenceRecordId, page, pageSize
        );

        // 组装分页信息
        Map<String, Object> response = new HashMap<>();
        response.put("data", orders);
        response.put("total", orders.size()); // 假设总数和查询结果一致
        response.put("page", page);
        response.put("pageSize", pageSize);
        return response;
    }

    /**
     * 生成追踪号
     *
     * @return 生成的追踪号
     */
    @PostMapping("/generateTrackingNumber")
    public Map<String, String> generateTrackingNumber() {
        String trackingNumber = outBoundOrderService.generateTrackingNumber();
        System.out.println(trackingNumber);
        Map<String, String> response = new HashMap<>();
        response.put("trackingNumber", trackingNumber);
        return response;
    }

    /**
     * 添加出库订单
     *
     * @param order 出库订单对象
     * @return 操作结果
     */
    @PostMapping("/add")
    public Map<String, Object> addOutBoundOrder(@RequestBody DelOutBoundOrder order) {
        boolean success = outBoundOrderService.insertOutBoundOrder(order);
        Map<String, Object> response = new HashMap<>();
        response.put("success", success);
        return response;
    }

    /**
     * 删除出库订单
     *
     * @param outId 出库单号
     * @return 操作结果
     */
    @DeleteMapping("/delete")
    public Map<String, Object> deleteOutBoundOrder(@RequestParam String outId) {
        boolean success = outBoundOrderService.deleteOutBoundOrder(outId);
        Map<String, Object> response = new HashMap<>();
        response.put("success", success);
        return response;
    }

    /**
     * 修改出库订单
     *
     * @param order 出库订单对象
     * @return 操作结果
     */
    @PutMapping("/update")
    public Map<String, Object> updateOutBoundOrder(@RequestBody DelOutBoundOrder order) {
        boolean success = outBoundOrderService.updateOutBoundOrder(order);
        Map<String, Object> response = new HashMap<>();
        response.put("success", success);
        return response;
    }

    /**
     * 获取出库订单明细
     *
     * @param outId 出库单号
     * @return 出库订单明细列表
     */
    @GetMapping("/details")
    public Map<String, Object> getOutBoundOrderDetails(@RequestParam String outId) {
        List<DelOutBoundDetail> details = outBoundOrderService.getDetailsByExitRecordId(outId);
        Map<String, Object> response = new HashMap<>();
        response.put("data", details);
        return response;
    }
}
